const express = require('express')
const multer = require('multer')
const boom = require('boom')
const { UPLOAD_PATH } = require('../utils/constant')
const Result = require('../models/Result')
const Book = require('../models/Book')

const bookService = require('../service/book')

const {md5 , decode} = require('../utils')

const router = express.Router()

router.post(
  '/upload',
  multer({dest:`${UPLOAD_PATH}/book`}).single('file'),
  function(req, res, next){
    if(!req.file || req.file.length === 0){
      new Result('电子书上传失败').fail(res)
    } else {
      const book = new Book(req.file)
      book.parse()
        .then( book => {
          new Result(book,'上传电子书成功').success(res)
        })
        .catch( err => {
          next(boom.badImplementation(err))
        })
    }
  }
)

router.post(
  '/create',
  function(req, res, next){
    const decoded = decode(req)
    // console.log(decoded);
    if (decoded && decoded.username){
      const data = req.body
      data.username = decoded.username
      const book = new Book(null,data)
      // console.log(book);
      // bookService.insertContents(book).then(()=>{
      //   console.log('目录插入成功');
      // })
      bookService.insertBook(book).then((e)=>{
        console.log(e);
        new Result('电子书添加成功').success(res)
      }).catch(err =>{
        next(boom.badImplementation(err))
      })
    }
  }
)

router.post(
  '/update',
  function(req, res, next){
    const decoded = decode(req)
    // console.log(decoded);
    if (decoded && decoded.username){
      const data = req.body
      data.username = decoded.username
      const book = new Book(null,data)
      // console.log(book);
      // bookService.insertContents(book).then(()=>{
      //   console.log('目录插入成功');
      // })
      bookService.updateBook(book).then((e)=>{
        console.log(e);
        new Result('电子书修改成功').success(res)
      }).catch(err =>{
        next(boom.badImplementation(err))
      })
    }
  }
)

router.get(
  '/get',
  function(req, res, next){
    const { fileName } = req.query
    if(!fileName) {
      next(boom.badRequest(new Error('参数fileName不能为空')))
    } else {
      bookService.getBook(fileName).then(book => {
        console.log(book);
        new Result(book,'获取图书信息成功').success(res)
      }).catch(err => {
        next(boom.badImplementation(err))
      })
    }
  }
)

router.get(
  '/category',
  function(req, res, next){
    bookService.getCategory().then(category => {
      // console.log(category);
      new Result(category,'获取获取分类成功').success(res)
    }).catch(err => {
      next(boom.badImplementation(err))
    })
  }
  
)

router.get('/list',function(req, res, next){
  bookService.listBook(req.query).then( ({list, count,page,pageSize}) => {
    // console.log(list);
    new Result({list,count,page: +page,pageSize: +pageSize},'获取图书列表成功').success(res)
  }).catch(err => {
    next(boom.badImplementation(err))
  })
})

router.get(
  '/delete',
  function(req, res, next){
    const { fileName } = req.query
    if(!fileName) {
      next(boom.badRequest(new Error('参数fileName不能为空')))
    } else {
      bookService.deleteBook(fileName).then(() => {
        new Result('删除图书信息成功').success(res)
      }).catch(err => {
        next(boom.badImplementation(err))
      })
    }
  }
)

module.exports = router